/* The code examples copyrighted by Nokia Corporation that are included to
 * this material are licensed to you under following MIT-style License:
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "tpmessagesender.h"

#include <TelepathyQt4/Account>
#include <TelepathyQt4/AccountSet>
#include <TelepathyQt4/ContactMessenger>
#include <TelepathyQt4/Types>
#include <TelepathyQt4/AccountManager>
#include <TelepathyQt4/PendingSendMessage>
#include <TelepathyQt4/Debug>

#include <QVariantMap>
#include <QVariant>
#include <QDebug>
#include <QtCore/QCoreApplication>

TpMessageSender::TpMessageSender(const QString &connectionMgr, const QString &address, const QString &message):
    reqCm(connectionMgr),
    reqAddress(address),
    reqMsg(message)
{
    Tp::registerTypes();

    #ifndef QT_NO_DEBUG
       Tp::enableDebug(true);
       Tp::enableWarnings(true);
    #endif // QT_NO_DEBUG

    mAM = Tp::AccountManager::create();
    if (!mAM.isNull() && !mAM->isReady()) {
        connect(mAM->becomeReady(),
                SIGNAL(finished(Tp::PendingOperation *)),
                SLOT(onAccountManagerReady(Tp::PendingOperation *)));
    }
}

TpMessageSender::~TpMessageSender()
{
}

void TpMessageSender::onAccountManagerReady(Tp::PendingOperation *op)
{
    Q_UNUSED(op);

    // Filter account by given Connection Manager name
    QVariantMap filter;
    filter.insert(QLatin1String("cmName"), QVariant::fromValue(reqCm));
    Tp::AccountSetPtr accountSet = mAM->filterAccounts(filter);
    if (!accountSet->accounts().isEmpty()) {
        Tp::AccountPtr acc = accountSet->accounts().first();

        // Sending message
        m_messenger = Tp::ContactMessenger::create(acc, reqAddress);
         connect(m_messenger->sendMessage(reqMsg),
                 SIGNAL(finished(Tp::PendingOperation*)),
                 SLOT(onSendMessageFinished(Tp::PendingOperation*)));
    } else {
        qDebug() << "No account found for given connection manager name";
        QCoreApplication::exit(1);
    }

}

void TpMessageSender::onSendMessageFinished(Tp::PendingOperation *op)
{
    if (op->isError()) {
        qDebug() << "Error sending message:" << op->errorName() << "-" << op->errorMessage();
        QCoreApplication::exit(1);
        return;
    }

    Tp::PendingSendMessage *psm = qobject_cast<Tp::PendingSendMessage *>(op);
    qDebug() << "Message sent, token is" << psm->sentMessageToken();
    QCoreApplication::exit(0);

}
